<?php
namespace app\api\service\uniapp\shop;

use app\api\controller\Leshua;
use app\api\controller\Wxutils;
use app\api\model\ShopYiwu;
use app\api\model\ShopYiwuCart;
use app\api\model\UserStore;
use app\api\service\uniapp\goodscart\GoodsCart;
use app\api\service\uniapp\order\OrderService;
use app\common\controller\Api;

class Shop10 extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function getshop(){
        $user = session('authuser');
        //接收参数
        $uid = $this->request->param('uid');
        if(!$uid || empty($uid)){
            $this->error("参数错误");
        }
        $shop = UserStore::where('user_id',$uid)->find();
        $shop['user_ywq']=number_format($user['ywq'],2);
        if(!$shop){
            $this->error("店铺不存在");
        }
        $this->success("加载成功",$shop);
    }
    public function load_shop(){
        $user = session('authuser');
        //接收参数
        $shop_id = $this->request->param('shop_id');
        $page = $this->request->param('page',1);
        $size = $this->request->param('size',10);
        $cat_id = $this->request->param('cat_id');
        if(empty($cat_id)){
            $catwhere = "1=1";
        }else{
            $catwhere = "cat_id1=$cat_id";
        }
        if (empty($shop_id)){
            $shop_id = config('default_shop_id');
        }
        //dump($catwhere);
        //获取店铺信息
        $shop = UserStore::where('id',$shop_id)->with(['category'])->find();

        $shop['shop_img'] = config('img_url').str_replace('/public/','',$shop['store_img'][0]);

        $page = ($page-1)*$size;
        $goods = ShopYiwu::where(['shop_id'=>$shop_id,'is_check'=>1,'is_on_sale'=>1])->where($catwhere)->limit($page,$size)->select();
        foreach ($goods as $k=>$v){
            //判断是否有缩图
            if(empty($v['thumb_pic']) || strstr($v['thumb_pic'],'/thumb/')===false){
                $thumbimg = get_thumb_image(unserialize($v['main_pics'])[0],0,300);
                //halt($thumbimg);
                $v['thumb_pic'] = $thumbimg;
                ShopYiwu::where('id',$v['id'])->update(['thumb_pic'=>'/public'.$thumbimg]);
            }
            $goods[$k]['thumb_pic'] = config('img_url').$v['thumb_pic'];
            $cartnum = ShopYiwuCart::where(['user_id'=>$user['user_id'],'goods_id'=>$v['id']])->value('goods_num');
            $goods[$k]['cart_num'] = $cartnum==null?0:$cartnum;
        }
        $data['goodsList'] = $goods;
        $data['shop'] = $shop;
        $goodsCartService = new GoodsCart();
        $data['cart'] = $goodsCartService->loadGoodsCart($user['user_id'],$shop_id);
        $this->success("加载成功",$data);
    }

    public function shopqrcode(){
        $user = session('authuser');

        $shop = UserStore::where('user_id',$user['user_id'])->find();
        if(!$shop){
            $this->error("店铺不存在");
        }
        //加载带参数二维码
        $code = Wxutils::getWxACodeUnlimit("pages/mine/shop/shoppay/shoppay",$user['user_id']);
        if($code==null){
            $this->error("获取二维码失败",$shop);
        }
        $shop['shop_qrcode'] = config('img_url').$code;
        $this->success("加载成功",$shop);
    }

    public function shoppay(){
        $user = session('authuser');
        //接收参数
        $shop_user_id = $this->request->param('shop_user_id');
        $money = $this->request->param('money');
        $benefit_type = $this->request->param('benefit_type');
        $orderService = new OrderService();
        $res = $orderService->createOrder($user['user_id'],$shop_user_id,$money,$benefit_type);

        if($res['code']==1){ //进行支付
            $leshua = new Leshua();
            $leres = $leshua->apppay($res['order_sn'],$res['money'],$user['user_id'],1);
            if($leres['status']){
                $res['code']=1;
                $res['data'] = $leres['jspay_info'];
                $res['msg'] = "发起支付";
            }else{
                $res['code'] = -6;
                $res['msg'] = $leres['error_msg'];
            }
            return json_encode($res);;
        }else{
            return json_encode($res);
        }

    }
}